home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 688 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.2 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Question about file-scope constants...
  5. Date: 12 Mar 1996 19:40:59 PST
  6. Organization: Comp Sci, University of Melbourne
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4i089m$2b7@mulga.cs.mu.OZ.AU>
  9. References: <314100A9.41C6@turing.cs.stcloud.msus.edu>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 11 Mar 1996 03:56:38 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMUZD3Uy4NqrwXLNJAQG0PwH+K2KcuZc1vaGxbGRAUPiCxr9Gfdkcu5Ih
  14.     OLAWpVpTJWedjezrkXYAMRYmzy42+0VXcLZRez23MgZBuveWw2AMSQ==
  15.     =kNIL
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. David Held <dheld@turing.cs.stcloud.msus.edu> writes:
  19.  
  20. >I was under the impression that a const object of any type and its data
  21. >members remained constant throughout their scope (which should be the
  22. >whole file, in this case), and that those data members were correctly
  23. >accessible from within any function in that scope.
  24.  
  25. That's true, provided your program does not do anything which might
  26. constitute "undefined behaviour", such as dereferencing an
  27. uninitialized pointer.  However, from your description of the symptoms,
  28. it sounds like your program is invoking undefined behaviour at some
  29. point, in which case all bets are off.
  30.  
  31. The C++ standard allows implementations to detect such errors at
  32. run-time, but it does not require them to do so.  Compilers are not
  33. required to place const objects in read-only memory, so if your program
  34. assigns via a stray pointer, your const objects could be overwriten.
  35. (Practically speaking, I would suggest that you get hold of an
  36. implementation which does perform run-time checking.  They're very
  37. useful for debugging problems like this.)
  38.  
  39. >class Exception
  40. >{
  41. [...]
  42. >public:
  43. >  Exception(char *);
  44. [...]
  45. >};
  46. [...]
  47. >const Exception Memory_Error("Memory Error");
  48.  
  49. Incidentally, tying in with another thread in comp.std.c++,
  50. it is the frequency of code like the above which means that
  51. changing the type of string literals from `char []' to `const char []'
  52. would break lots of code.  I consider such code to be bad style --
  53. the constructor for exception should take `const char *', not `char *' --
  54. but until all compilers warn about it, such code will remain frequent,
  55. even in newly developed C++-style code that makes full use of new features
  56. such as exceptions, templates, namespaces, etc.
  57.  
  58. >Here's my problem: I declare several const Exceptions in the exception.h
  59. >file, since they are ones that I expect to use in every program
  60. >(Memory_Error, Boundary_Error, File_Error, etc.).
  61.  
  62. Note that in C++, declaring a global variable `const' also has the side
  63. effect of giving that variable default internal linkage, just as if it was
  64. declared `static'.  If you want a const to have external linkage, you
  65. need to explicitly declare it `extern'.
  66.  
  67. >I wrote a demo program where the main body is in a file called
  68. >main.cpp, and the exception testing demo is in exception_demo.cpp.  I
  69. >have appropriate header files for both, and I include exception.h in
  70. >both (I set a meta-variable to make sure exception.h only gets included
  71. >once).  Exception_demo.cpp contains the testing function, but apparently
  72. >when the program enters this only function of exception_demo.cpp, one of
  73. >the exceptions declared in exception.h gets mysteriously modified.
  74.  
  75. This effect which you observed in the debugger may be due to a different copy
  76. of the exception coming into scope when you enter a function in a different
  77. translation unit.  As noted above, global consts default to internal linkage,
  78. so you will get one copy of each global const for every translation unit.
  79.  
  80. >There is no conceivable way that my code could be modifying the object data.
  81.  
  82. Famous last words ;-)
  83.  
  84. In C and C++, there are hundreds of conceivable ways for code to have
  85. undefined behaviour.
  86.  
  87. --
  88. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  89. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  90. ---
  91. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  92.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  93.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  94.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  95.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  96. ]
  97.